<?php
	class VideoAction extends CommonAction{
		
		public function index(){
			$model = M('Archives');
			$map = array('type'=> 'video');
			$count = $model->where($map)->count('id');
	        import("ORG.Util.Page");
			$p = new Page($count, C('LIST_BOARD_NUMBER'));
	        //分页查询数据
	        $list = $model
	        	->where($map)->order('id DESC')
	        	->limit($p->firstRow . ',' . $p->listRows)->select();
			
			$this->assign('list', $list);
	        $this->assign("page", $p->show());
	        Cookie::set('_currentUrl_', __SELF__);
			$this->display();
		}
		
		public function edit(){
			$model = M('Archives');
	        //$id = $_REQUEST[$model->getPk()];
	        $vo = $model->find($_GET['id']);
	        $this->assign('vo', $vo);
	        $this->display();
		}
		
		public function getUploadInfo(){
			if ( isset( $_SESSION['video_info'] ) ){
				$html = '<tr id="uploadInfo">
						<td align="right"></td>
						<td align="left">
						<input type="text" name="video_path" class="input_text w480" value="'.$_SESSION["video_info"]["savename"].'" />
						&nbsp;&nbsp;&nbsp;
						<input type="button" class="submit" onclick="rmvideo(\''.$_SESSION['video_info']['savepath'].$_SESSION['video_info']['savename'].'\');" value="移除此文件，若重新上传则自动删除" />
						</td>
						</tr>';
				echo $html;
			}else{
				echo 0;
			}
		}
		
		public function insert(){
			unset( $_POST["file_upload"] );
			$_POST['create_time'] = time();
			$_POST['author'] = $_SESSION[C('USER_AUTH_KEY')];
			if ( !empty( $_POST['thumb'] ) ){
				$thumb_array = explode( '/' , $_POST['thumb'] );
				$_POST['thumb'] = $thumb_array[count( $thumb_array ) - 2] . '/' . $thumb_array[count( $thumb_array ) - 1];
			}
			$model = D('Archives');
			if ( $insert = $model->add( $_POST ) ){
				if ( isset( $_SESSION['video_info'] ) ) {
					unset( $_SESSION['video_info'] );
				}
				D('Category')->where("id='".$_POST['cid']."'")->setInc('count');
				$this->saveTag($_POST['tags'],$insert); //处理tags
				$this->checkSession(); 
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				$this->success ('添加成功!');
			}else{
				$this->error('添加失敗!');
			}
		}
		
		public function update(){
			unset( $_POST["file_upload"] );
			$_POST['update_time'] = time();
			if ( !empty( $_POST['thumb'] ) ){
				$thumb_array = explode( '/' , $_POST['thumb'] );
				$_POST['thumb'] = $thumb_array[count( $thumb_array ) - 2] . '/' . $thumb_array[count( $thumb_array ) - 1];
			}
			$model = D('Archives');
			if ( $_POST['video_path'] ){
				$video_path = $model->where("id='".$_POST['id']."'")->find();
				$video_o = './'.C('WEB_PUBLIC_PATH').'/'.C('DIR_ATTCH_PATH').'/'.$video_path['video_path'];
			}
			if ( $model->where("id='".$_POST['id']."'")->save( $_POST ) ){
				if ( isset( $_SESSION['video_info'] ) ) {
					if ( file_exists( $video_o ) ){
						unlink( $video_o );
					}
					unset( $_SESSION['video_info'] );
				}
				$this->saveTag($_POST['tags'],$_POST['id']); //处理tags
				$this->checkSession(); 
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				$this->success ('修改成功!');
			}else{
				$this->error('修改失敗!');
			}
		}
		
		/**
		 * ===================================================
		 * 删除视频文档
		 *
		 * ===================================================
		 */
		public function delete(){
			if ( isset( $_GET['id'] ) ){
				$this->eDelete( 'video' , $_GET['id'] );
			}
		}
		
		public function _before_add()
		{
			$this->getCategoryOption('video');
		}
		
		public function _before_edit(){
			$this->getCategoryOption('video', $_GET['cid']);
		}
		
	}
